using AvalonAdminDto.Common;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AvalonAdmin.Converters
{
    public class TextConvert
    {
        #region 销售状态转换
        public static int ConvertSaleStatus(string status)
        {
            switch (status)
            {
                case "正销": return 0; break;
                case "停销": return 1; break;
                case "停产": return 2; break;
            }
            return -1;
        }

        public static string ConvertSaleStatusText(int? status)
        {
            switch (status)
            {
                case 0: return "正销"; break;
                case 1: return "停销"; break;
                case 2: return "停产"; break;
            }
            return "";
        }
        #endregion

        #region 车辆类型
        public static int ConvertVehicleType(string status)
        {
            switch (status)
            {
                case "普通车型": return 0; break;
                case "销售车型": return 1; break;
                case "精准车型": return 2; break;
            }
            return -1;
        }

        public static string ConvertVehicleTypeText(int? status)
        {
            switch (status)
            {
                case 0: return "普通车型"; break;
                case 1: return "销售车型"; break;
                case 2: return "精准车型"; break;
            }
            return "";
        }

        #endregion

        #region Excel字段名和实体名转换
        /// <summary>
        /// 加载字段对应
        /// </summary>
        /// <param name="importType"></param>
        public static Dictionary<string, string> LoadModelDic(ImportType importType)
        {
            try
            {
                string json = "";
                string filePath = "";
                
                // 获取应用程序基目录
                string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
                Console.WriteLine($"[DEBUG] LoadModelDic - 应用程序基目录: {baseDirectory}");
                
                // 根据导入类型确定对应的映射文件路径
                switch (importType)
                {
                    case ImportType.Admin:
                        filePath = Path.Combine(baseDirectory, "ImportJson", "AdminMapping.json");
                        break;
                    case ImportType.Area:
                        filePath = Path.Combine(baseDirectory, "ImportJson", "AreaMapping.json");
                        break;
                    case ImportType.Brand:
                        filePath = Path.Combine(baseDirectory, "ImportJson", "BrandMapping.json");
                        break;
                    case ImportType.Country:
                        filePath = Path.Combine(baseDirectory, "ImportJson", "CountryMapping.json");
                        break;
                    case ImportType.Car:
                        filePath = Path.Combine(baseDirectory, "ImportJson", "CarMapping.json");
                        break;
                    case ImportType.CarType:
                        // CarType使用Convert文件，保持原有逻辑
                        filePath = Path.Combine(baseDirectory, "ImportJson", "CarTypeConvert.json");
                        break;
                    case ImportType.CarTypeVersion:
                        filePath = Path.Combine(baseDirectory, "ImportJson", "CarTypeVersionMapping.json");
                        break;
                    case ImportType.PreciseCartType:
                        filePath = Path.Combine(baseDirectory, "ImportJson", "PreciseCartTypeMapping.json");
                        break;
                    case ImportType.SalesCarType:
                        filePath = Path.Combine(baseDirectory, "ImportJson", "SalesCarTypeMapping.json");
                        break;
                    case ImportType.VehicleSeries:
                        // VehicleSeries使用Convert文件，保持原有逻辑
                        filePath = Path.Combine(baseDirectory, "ImportJson", "VehicleSeriesConvert.json");
                        break;
                    case ImportType.CarParts:
                        filePath = Path.Combine(baseDirectory, "ImportJson", "CarPartsMapping.json");
                        break;
                    case ImportType.CarPartsType:
                        filePath = Path.Combine(baseDirectory, "ImportJson", "CarPartsTypeMapping.json");
                        break;
                    case ImportType.VehicleSeriesCarPartsType:
                        filePath = Path.Combine(baseDirectory, "ImportJson", "VehicleSeriesCarPartsTypeMapping.json");
                        break;
                    case ImportType.Rule:
                        filePath = Path.Combine(baseDirectory, "ImportJson", "RuleConfigMapping.json");
                        break;
                    default:
                        System.Diagnostics.Debug.WriteLine($"LoadModelDic: 未支持的导入类型 {importType}");
                        return new Dictionary<string, string>();
                }

                // 检查文件是否存在
                Console.WriteLine($"[DEBUG] LoadModelDic - 当前工作目录: {Directory.GetCurrentDirectory()}");
                Console.WriteLine($"[DEBUG] LoadModelDic - 尝试读取文件: {filePath}");
                Console.WriteLine($"[DEBUG] LoadModelDic - 文件完整路径: {Path.GetFullPath(filePath)}");
                Console.WriteLine($"[DEBUG] LoadModelDic - 文件是否存在: {File.Exists(filePath)}");
                
                if (!File.Exists(filePath))
                {
                    Console.WriteLine($"[DEBUG] LoadModelDic - 文件不存在，返回null");
                    return null;
                }

                json = File.ReadAllText(filePath);
                
                if (string.IsNullOrWhiteSpace(json))
                {
                    return null;
                }

                Dictionary<string, string> keyValuePairs = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
                
                // 确保返回值不为null
                var result = keyValuePairs ?? new Dictionary<string, string>();
                System.Diagnostics.Debug.WriteLine($"LoadModelDic: 成功加载 {result.Count} 个字段映射，类型: {importType}");
                return result;
            }
            catch (Exception ex)
            {
                // 记录错误但不抛出异常，返回空字典
                System.Diagnostics.Debug.WriteLine($"LoadModelDic error: {ex.Message}, ImportType: {importType}");
                return null;
            }
        }
        #endregion
    }
}
